#include "handshake_handler.hpp"

#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <boost/foreach.hpp>

handshake_handler::handshake_handler(const std::string& server_origin, const std::string& server_location) :
    _server_origin(server_origin),
    _server_location(server_location)
{
}

std::string handshake_handler::handle_shake(const std::string& handshake)
{
    return handle_shake_06(handshake);
}

std::string handshake_handler::handle_shake_06(const std::string& handshake)
{
    std::string key = get_handshake_line(handshake, "Sec-WebSocket-Key: ");
    static const std::string magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    key += magic;
    unsigned char hash_key[key.size()];
    memcpy(hash_key, key.c_str(), key.size());
    unsigned char output[20];
    _sha.CalculateDigest(output, hash_key, key.size());

    std::string final_output = base64_encode(output, 20);

    std::stringstream server_shake;
    server_shake <<
        "HTTP/1.1 101 Switching Protocols\r\n" <<
        "Upgrade: websocket\r\n" <<
        "Connection: Upgrade\r\n" <<
        "Sec-WebSocket-Accept: " << final_output << "\r\n" <<
        "\r\n";

    return server_shake.str();
}

// this useful function from: http://www.adp-gmbh.ch/cpp/common/base64.html
std::string handshake_handler::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
{
    static const std::string base64_chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789+/";
    std::string ret;
    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];

    while (in_len--) {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3) {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for(i = 0; (i <4) ; i++)
                ret += base64_chars[char_array_4[i]];
            i = 0;
        }
    }

    if (i)
    {
        for(j = i; j < 3; j++)
            char_array_3[j] = '\0';

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
        char_array_4[3] = char_array_3[2] & 0x3f;

        for (j = 0; (j < i + 1); j++)
            ret += base64_chars[char_array_4[j]];

        while((i++ < 3))
            ret += '=';
    }

    return ret;
}

std::string handshake_handler::get_handshake_line(const std::string& message, const std::string& header)
{
    std::string temp = message.substr(message.find(header) + header.size());
    temp = temp.substr(0, temp.find("\r\n"));
    return temp;
}
